package io.codegeneratetool.vo;

import io.codegeneratetool.util.JPAUtil;
import io.codegeneratetool.util.validate.ValidateAnnotationUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Transient;

 
/**
 * 说明： 字段(属性) 信息
 */
public class FieldInfo {
	
	/**  被处理的字段 */
	private Field field;
	

	/**  实体类型名称 */
	private String className="";
	
	
	/**  字段名称 */
	private String fieldName="";
	
	
	/**  css 类型名称 */
	private String cssClass="";
	
	private HashMap<String, String> customDOMAttributes = new HashMap<String, String>();
 
	
	/**  验证属性,配合前端验证框架 */
	private List<String> validateAttributes = new ArrayList<String>();

	
	public boolean isBoolean(){
		String lowerCase = this.field.getType().getSimpleName().toLowerCase();
		return "boolean".equals(lowerCase);
	}
	
	
	public boolean isEnum(){
		Class<?> type = this.field.getType(); 
		return type.isEnum();
	}
	public boolean isClob(){
		Lob type = this.field.getAnnotation(Lob.class); 
		String lowerCase = this.field.getType().getSimpleName().toLowerCase();
		
		return type!=null && "string".equals(lowerCase);
	}
	
	public boolean isString(){ 
		String lowerCase = this.field.getType().getSimpleName().toLowerCase();
		
		return "string".equals(lowerCase);
	}
	
	public boolean isBlob(){
		Lob type = this.field.getAnnotation(Lob.class); 
		String lowerCase = this.field.getType().getSimpleName().toLowerCase();
		
		return type!=null && lowerCase.contains("byte");
	}
	
	/** 是否瞬时变量
	 * @return
	 */
	public boolean isTransient(){
		Transient type = this.field.getAnnotation(Transient.class); 
		return type!=null;
	}
	
	/** 是否主键
	 * @return
	 */
	public boolean isDBID(){
		Id type = this.field.getAnnotation(Id.class); 
		return type!=null;
	}
	
	/** 获取字段名称
	 * @return
	 */
	public String getColumnName(){
		Column column = this.field.getAnnotation(Column.class); 
		if(column==null || column.name()==null){
			return this.field.getName().toUpperCase();
		}
		return column.name();
	}
	
	/**获取grid中，显示值位置情况规则。
	 * 文本居左
	 * 数字居右
	 * 字典居中  2013-7-5 由需求定义
	 * 
	 * 
	 * @return
	 */
	public String getGridCellAlign(){
		String lowerCase = this.field.getType().getSimpleName().toLowerCase();
		if("int".equals(lowerCase)
				|| "interger".equals(lowerCase)
				|| "long".equals(lowerCase)
				|| "float".equals(lowerCase)
				|| "bigdecimal".equals(lowerCase)
				|| "double".equals(lowerCase)
				){
			return "right";
		} 
		return "left";
	}
	
	
	
	/** 获取getter方法名字
	 * 
 	 * @return
	 */
	public String getGetter(){
		return "get"+ fieldName.substring(0,1).toUpperCase()+ fieldName.substring(1);
				 
	}
	
	
	/**
	 * 说明：为html元素增加自定义属性
	 * @author jiaxf 创建时间2013-7-25下午2:09:40
	 * @param 
	 */
	public void addCustomAttribute(String attrName , String attrValue){
		customDOMAttributes.put(attrName, attrValue);
	}
	
	public String getCustomAttributes(){
		StringBuilder builder = new StringBuilder();
		builder.append(" ");
		for(Entry<String, String> entry: customDOMAttributes.entrySet()){
			builder.append(" "+ entry.getKey()+"=\""+ entry.getValue() +"\" ");
		}
		builder.append(" ");
		return builder.toString();
	}
	
	
	/**
	 * 说明：是否数字类型
	 * 涉及到 freemarker 的数字格式化. 默认的, 是 1,000  用 ?c 输出 1000
	 * @author jiaxf 创建时间2012-11-9下午12:06:27
	 * @param 
	 */
	public boolean isInteger(){
		String lowerCase = this.field.getType().getSimpleName().toLowerCase();
		if("integer".equals(lowerCase)
				|| "long".equals(lowerCase)
 
				){
			return true;
		} 
		return false;
	}
	
	public boolean isTimestamp(){
		String lowerCase = this.field.getType().getName().toLowerCase();
		if("java.sql.timestamp".equals(lowerCase) ){
			return true;
		} 
		return false;
	}
	
	public boolean isDate(){
		String lowerCase = this.field.getType().getName().toLowerCase();
		if("java.util.date".equals(lowerCase) ){
			return true;
		} 
		return false;
	}
	
	
	
	/**
	 * 说明：获取 html输入区的ID
	 * @author jiaxf 创建时间2012-10-25下午1:26:05
	 * @param 
	 */
	public String getHtmlInputId(){
		String string = className+ fieldName.substring(0,1).toUpperCase()+ fieldName.substring(1);
		String[] spl = string.split("\\.");
		if(spl.length>1){
			for (int i = 0; i < spl.length; i++) {
				if(i==0){
					string = spl[0];
				}else{
					string += spl[i].substring(0,1).toUpperCase()+ spl[i].substring(1);
				}
			}
		} 
		return string;
	}
	
 
	
	 
	
	public String generateValidateInfo(Object obj){
		if(this.field==null){  //这里基本不可能了
			return "";
		}
		ValidateAnnotationUtil util = new ValidateAnnotationUtil();
		return util.process(obj, field);  // 返回json
	}
	
	
	
	
	/**  
	 * 获取field  
	 * @return field field  
	 */
	public Field getField() {
		return field;
	}




	/**
	 * 设置field  
	 *	@param field field  
	 */
	public void setField(Field field) {
		this.field = field;
	}




	/**
	 * 说明：获取input name属性值
	 * @author jiaxf 创建时间2012-10-19上午9:28:54
	 * @param 
	 */
	public String getInputName(){
		return className+"."+ JPAUtil.processJPAFieldName(this.field);
	}
	
	
	public String getFreemarkerCondition(){
		
		String jpaFieldname =  JPAUtil.processJPAFieldName(this.field); 
		String freemarkerPrefix = "<#if "+ className +"?? ";
		String[] split = jpaFieldname.split("\\.");
		String tmp = className;
		for (String string : split) {
			tmp = className +"."+ string;
			freemarkerPrefix = freemarkerPrefix +" && "+ tmp +"?? "; 
		}
		return freemarkerPrefix + " >";
	}
	
	

	/**  
	 * 获取className  
	 * @return className className  
	 */
	public String getClassName() {
		return className;
	}


	/**
	 * 设置className  
	 *	@param className className  
	 */
	public void setClassName(String className) {
		this.className = className;
	}


	/**  
	 * 获取fieldName  
	 * @return fieldName fieldName  
	 */
	public String getFieldName() {
		return fieldName;
	}


	/**
	 * 设置fieldName  
	 *	@param fieldName fieldName  
	 */
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}


	/**  
	 * 获取cssClass  
	 * @return cssClass cssClass  
	 */
	public String getCssClass() {
		return cssClass;
	}


	/**
	 * 设置cssClass  
	 *	@param cssClass cssClass  
	 */
	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}


	/**  
	 * 获取validateAttributes  
	 * @return validateAttributes validateAttributes  
	 */
	public List<String> getValidateAttributes() {
		return validateAttributes;
	}


	/**
	 * 设置validateAttributes  
	 *	@param validateAttributes validateAttributes  
	 */
	public void setValidateAttributes(List<String> validateAttributes) {
		this.validateAttributes = validateAttributes;
	}
	
	
	
	
	
}
